package com.huike.pmps.controller;

import com.huike.pmps.model.dto.common.PageInfo;
import com.huike.pmps.model.entity.Member;
import com.huike.pmps.model.vo.Result;
import com.huike.pmps.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.util.function.Tuple2;


/**
 * 作者：谌贵斌(黑桃K)
 * 日期: 2888-08-08
 * 老师QQ: 272488352
 * 官方QQ交流群: 377748272
 * 刀客程序员官网：http://www.daoke360.com
 * 刀客程序员淘宝旗舰店：https://daoke360.taobao.com/
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/api/members")
public class MemberController {

    @Autowired
    MemberService memberService;


    @ApiOperation(value = "添加用户", httpMethod = "POST")
    @PostMapping
    public Result insertOne(Member member) {
        return memberService.insertOne(member);
    }

    @ApiOperation(value = "用户信息修改", httpMethod = "PATCH")
    @PatchMapping
    public Result updateById(Integer id, String memberName, String name, String mobile, String email, Short sex,
                             Integer regionId, String regionName, Integer provinceId, String provinceName,String password) {
        Member member = Member.builder().id(id).memberName(memberName).name(name).mobile(mobile).email(email).sex(sex)
                .regionId(regionId).regionName(regionName).provinceId(provinceId).provinceName(provinceName).password(password).build();
        return memberService.updateById(member);
    }

    @ApiOperation(value = "根据用户ID查询单个用户信息", httpMethod = "GET")
    @GetMapping("/{memberId}")
    public Result selectOneById(@PathVariable("memberId") int memberId) {
        return memberService.selectOneByMemberId(memberId);
    }

    @ApiOperation(value = "按搜索条件分页查询", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberName", value = "账号", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "姓名", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sex", value = "性别", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "state", value = "用户状态", paramType = "query", dataType = "int")
    })
    @GetMapping
    public Result selectByCondition(PageInfo pageInfo, String memberName, String name, Short sex, Short state) {
        return memberService.selectByCondition(pageInfo, memberName, name, sex, state);
    }

    @ApiOperation(value = "用户启用或禁用", httpMethod = "PATCH")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "用户状态", paramType = "query", dataType = "int", required = true),
    })
    @PatchMapping("/state/{memberId}")
    public Result updateMemberStateByMemberId(@PathVariable("memberId") Integer memberId, Short state) {
        return memberService.updateMemberStateByMemberId(memberId, state);
    }

    @ApiOperation(value = "用户密码更改", httpMethod = "PATCH")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "newPassword", value = "用户新密码", paramType = "query", dataType = "String", required = true)
    })
    @PatchMapping("/password/{memberId}")
    public Result updateMemberPasswordByMemberId(@PathVariable("memberId") Integer memberId, String newPassword) {
        return memberService.updateMemberPasswordByMemberId(memberId, newPassword);
    }


    @ApiOperation(value = "根据用户ID删除用户", httpMethod = "DELETE")
    @DeleteMapping("/{memberId}")
    public Result deleteByMemberId(@PathVariable("memberId") Integer memberId) {
        return memberService.deleteByMemberId(memberId);
    }


    @ApiOperation(value = "给用户分配角色", httpMethod = "POST")
    @ApiImplicitParam(name = "roleIds", value = "角色ID集合(角色ID按 逗号 拼接)", paramType = "query", dataType = "String")
    @PostMapping("/user_role/{memberId}")
    public Result insertMemberAndRoleRelation(@PathVariable("memberId") Integer memberId, String roleIds) {
        return memberService.insertMemberAndRoleRelation(memberId, roleIds);
    }

}
